package com.example.csdn.controller;

import com.example.csdn.DTO.ArticleAddDTO;
import com.example.csdn.pojo.Article;
import com.example.csdn.pojo.PageBean;
import com.example.csdn.pojo.Result;
import com.example.csdn.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @GetMapping
    public Result<PageBean<Article>> articleList(
        Integer pageNum, Integer pageSize,
        @RequestParam(required = false) Integer categoryId,
        @RequestParam(required = false) String state
    ){
        PageBean<Article> pb = articleService.articleList(pageNum, pageSize, categoryId, state);
        return Result.success(pb);
    }

    @PostMapping
    public Result add(@RequestBody @Validated ArticleAddDTO articleAddDTO){
        articleService.add(articleAddDTO);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody @Validated Article article){
        articleService.update(article);
        return Result.success();
    }

    @DeleteMapping
    public Result delete(Integer id){
        articleService.delete(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getArticleById(@PathVariable Integer id){
        Article article = articleService.getArticleById(id);
        return Result.success(article);
    }
}
